﻿namespace TheCodeWerks.Advertising
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.Linq;
	using System.Windows;
	using System.Windows.Markup;
	using Google.AdMob.Ads.WindowsPhone7.WPF;
	using GoogleGender = Google.AdMob.Ads.WindowsPhone7.Gender;
	using GpsLocation = Google.AdMob.Ads.WindowsPhone7.GpsLocation;

	public class AdMobProvider : IAdvertisingProvider
	{
		private const string TestDeviceId = "89A5E633-DD48-4A9A-AC2F-652F78D03F8C";

		public AdvertisingConfiguration Config { get; set; }

		public AdProvider Provider { get { return AdProvider.AdMob; } }

		public FrameworkElement CreateControl()
		{
			var user = Config.User;
			var settings = Config.Providers[Provider];

			var control = new BannerAd()
			{
				AdUnitID = settings.Id,
				Language = XmlLanguage.GetLanguage(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
			};

			control.BeginUpdates();

			if (Config.TestMode)
			{
				control.TestDeviceIDs.Add(TestDeviceId);
			}

			control.AdReceived += (source, args) =>
			{
				((AdEvents)((BannerAd)source).Tag).AdReady();
				((BannerAd)source).BeginUpdates();
			};

			control.AdFailed += (source, args) =>
			{
				((AdEvents)((BannerAd)source).Tag).NotFilled();
				((BannerAd)source).BeginUpdates();
			};

			if (user != null)
			{
				if (user.Latitude != null && user.Longitude != null)
				{
					control.GpsLocation = new GpsLocation() { Latitude = user.Latitude.Value, Longitude = user.Longitude.Value };
				}
				else
				{
					string description = null;

					if (!string.IsNullOrEmpty(user.City)
						&& !string.IsNullOrEmpty(user.State)
						&& !string.IsNullOrEmpty(user.Zip)
						&& !string.IsNullOrEmpty(user.Country))
					{
						description = "{0}, {1} {2}, {3}";
					}

					else if (!string.IsNullOrEmpty(user.City)
						&& !string.IsNullOrEmpty(user.State)
						&& !string.IsNullOrEmpty(user.Zip)
						&& string.IsNullOrEmpty(user.Country))
					{
						description = "{0}, {1} {2}";
					}

					else if (!string.IsNullOrEmpty(user.City)
						&& !string.IsNullOrEmpty(user.State)
						&& string.IsNullOrEmpty(user.Zip)
						&& !string.IsNullOrEmpty(user.Country))
					{
						description = "{0}, {1}, {3}";
					}

					else if (string.IsNullOrEmpty(user.City)
						&& !string.IsNullOrEmpty(user.State))
					{
						description = "{1}";
					}

					if (description != null)
					{
						control.LocationDescription = string.Format
						(
							description,
							user.City,
							user.State,
							user.Zip,
							user.Country
						);
					}
				}

				if (user.Gender != Gender.Unknown)
				{
					control.Gender = user.Gender == Gender.Male ? GoogleGender.Male : GoogleGender.Female;
				}

				if (user.Birthdate != null)
				{
					control.Birthday = user.Birthdate;
				}
			}

			return control;
		}

		public void ConfigureControl(FrameworkElement element, AdUnit unit)
		{
			var banner = (BannerAd)element;
			banner.Keywords.Clear();

			var keywords = new List<string>(Config.DefaultKeywords.Count + unit.Keywords.Count);
			keywords.AddRange(Config.DefaultKeywords);
			keywords.AddRange(unit.Keywords);

			keywords.ForEach(item => banner.Keywords.Add(item));
			banner.KeywordList = keywords;
		}

		public void RequestNext(FrameworkElement element)
		{
			((BannerAd)element).EndUpdates();
			//((BannerAd)control).ShowNewAd();			
		}
	}
}
